﻿<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">

    <script type='text/html' template lay-done="BindRole();">
        <input type='hidden' name='PID' id="PID" value="{{ d.params.PID || '' }}" />
    </script>
    <div class='layui-form-item'>
        <label class='layui-form-label'>用户名</label>
        <div class='layui-input-inline'>
            <script type='text/html' template>
                <input type='text' readonly="readonly" name='UserID' value='{{ d.params.UserID || '' }}' lay-verify='required' placeholder='用户名' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <div id="RolesCheckBox"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline">
            <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" value="确认" onclick="save();" class="layui-btn">
        </div>
    </div>
</div>

<script type="text/javascript">
    layui.use(['form', 'admin'], function () {
        var $ = layui.$;
        var form = layui.form;
        var admin = layui.admin;
    });
    function BindRole() {
        var $ = layui.$;
        var admin = layui.admin;
        var form = layui.form;
        admin.req({
            url: layui.setter.APIUrl + '/admin/right_RoleMgr/GetUserRole', //实际使用请改成服务端真实接口
            data: { "userpid": $("#PID").val() },
            type: 'post',
            done: function (res) {
                layui.each(res.Data.rows, function (index, eachdata) {
                    var strIsSel = '';  
                    if (res.Data.roles.indexOf(eachdata.PID) >= 0)
                        strIsSel = "checked=''";
                    $("#RolesCheckBox").append("<input type='checkbox' name='ckRoleID' title='" + eachdata.RoleName + "' value='" + eachdata.PID + "' " + strIsSel + ">");
                });
                form.render();
            }
        });
    }

    function save() {
        debugger
        var $ = layui.$;
        var admin = layui.admin;
        var RoleIds = "";
        $("input[name='ckRoleID']:checked").each(function (i) {
            RoleIds += "," + $(this).val();
        })
        if (RoleIds.indexOf(",") == 0)
            RoleIds = RoleIds.substring(1);

        var html = $("#RolesCheckBox").html();
        if (html == "") {
            return false;
        }
        admin.req({
            url: layui.setter.APIUrl + '/admin/right_RoleMgr/SetUserRole', //实际使用请改成服务端真实接口 ,
            data: { "userpid": $("#PID").val(), "RoleIDs": RoleIds },
            type: 'post',
            done: function (res) {
                layer.msg(res);
                layer.closeAll();//执行关闭 
            }
        });
    }
</script>
